home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / Basic / Visual Basic.60 / COMMON / TOOLS / VCM / VCM.MDB / VcmComponentContainer / 08_Cabinet / MYPROVIDERRS.H < prev    next >
Encoding:
C/C++ Source or Header  |  1998-05-18  |  8.7 KB  |  267 lines

  1. // MyProviderRS.h : Declaration of the CMyProviderRowset
  2. //
  3. // This is a part of the ActiveX Template Library.
  4. // Copyright (C) 1998 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // ActiveX Template Library Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // ActiveX Template Library product.
  12.  
  13. #ifndef __CMyProviderRowset_H_
  14. #define __CMyProviderRowset_H_
  15.  
  16. #include <stdio.h>
  17. #include <tchar.h>
  18. #include "rowloc.h"
  19. #include "resource.h"       // main symbols
  20.  
  21. class CAgentRowset;
  22. class CMyProviderCommand;
  23.  
  24. class CAgentMan
  25. {
  26. public:
  27.     DWORD dwBookmark;
  28.     TCHAR szCommand[256];
  29.     TCHAR szText[256];
  30.     TCHAR szCommand2[256];
  31.     TCHAR szText2[256];
  32.  
  33.     static ATLCOLUMNINFO* GetColumnInfo(CAgentRowset* pThis, ULONG* pcCols);
  34.     static ATLCOLUMNINFO* GetColumnInfo(CMyProviderCommand* pThis, ULONG* pcCols);
  35.  
  36. };
  37.  
  38. #define ADD_COLUMN_ENTRY(ulCols, name, ordinal, colSize, type, precision, scale, guid, dataClass, member) \
  39.     _rgColumns[ulCols].pwszName = (LPOLESTR)name; \
  40.     _rgColumns[ulCols].pTypeInfo = (ITypeInfo*)NULL; \
  41.     _rgColumns[ulCols].iOrdinal = (ULONG)ordinal; \
  42.     _rgColumns[ulCols].dwFlags = 0; \
  43.     _rgColumns[ulCols].ulColumnSize = (ULONG)colSize; \
  44.     _rgColumns[ulCols].wType = (DBTYPE)type; \
  45.     _rgColumns[ulCols].bPrecision = (BYTE)precision; \
  46.     _rgColumns[ulCols].bScale = (BYTE)scale; \
  47.     _rgColumns[ulCols].cbOffset = offsetof(dataClass, member); \
  48.     memset(&(_rgColumns[ulCols].columnid), 0, sizeof(DBID)); \
  49.     _rgColumns[ulCols].columnid.uName.pwszName = (LPOLESTR)name;
  50.  
  51.  
  52.  
  53. #define ADD_COLUMN_ENTRY_EX(ulCols, name, ordinal, colSize, type, precision, scale, guid, dataClass, member, flags) \
  54.     _rgColumns[ulCols].pwszName = (LPOLESTR)name; \
  55.     _rgColumns[ulCols].pTypeInfo = (ITypeInfo*)NULL; \
  56.     _rgColumns[ulCols].iOrdinal = (ULONG)ordinal; \
  57.     _rgColumns[ulCols].dwFlags = flags; \
  58.     _rgColumns[ulCols].ulColumnSize = (ULONG)colSize; \
  59.     _rgColumns[ulCols].wType = (DBTYPE)type; \
  60.     _rgColumns[ulCols].bPrecision = (BYTE)precision; \
  61.     _rgColumns[ulCols].bScale = (BYTE)scale; \
  62.     _rgColumns[ulCols].cbOffset = offsetof(dataClass, member); \
  63.     memset(&(_rgColumns[ulCols].columnid), 0, sizeof(DBID)); \
  64.     _rgColumns[ulCols].columnid.uName.pwszName = (LPOLESTR)name;
  65.  
  66.  
  67.  
  68. // CMyProviderCommand
  69. class ATL_NO_VTABLE CMyProviderCommand :
  70.     public CComObjectRootEx<CComMultiThreadModel>,
  71.     public IAccessorImpl<CMyProviderCommand>,
  72.     public ICommandTextImpl<CMyProviderCommand>,
  73.     public ICommandPropertiesImpl<CMyProviderCommand>,
  74.     public IObjectWithSiteImpl<CMyProviderCommand>,
  75.     public IConvertTypeImpl<CMyProviderCommand>,
  76.     public IColumnsInfoImpl<CMyProviderCommand>
  77.  
  78. {
  79. public:
  80. BEGIN_COM_MAP(CMyProviderCommand)
  81.     COM_INTERFACE_ENTRY(ICommand)
  82.     COM_INTERFACE_ENTRY(IObjectWithSite)
  83.     COM_INTERFACE_ENTRY(IAccessor)
  84.     COM_INTERFACE_ENTRY(ICommandProperties)
  85.     COM_INTERFACE_ENTRY(IConvertType)
  86.     COM_INTERFACE_ENTRY(IColumnsInfo)
  87.     COM_INTERFACE_ENTRY2(ICommandText, ICommand)
  88.     COM_INTERFACE_ENTRY(IColumnsInfo)
  89.     COM_INTERFACE_ENTRY(IConvertType)
  90. END_COM_MAP()
  91. // ICommand
  92. public:
  93.     HRESULT FinalConstruct()
  94.     {
  95.         HRESULT hr = CConvertHelper::FinalConstruct();
  96.         if (FAILED (hr))
  97.             return hr;
  98.         hr = IAccessorImpl<CMyProviderCommand>::FinalConstruct();
  99.         if (FAILED(hr))
  100.             return hr;
  101.         return CUtlProps<CMyProviderCommand>::FInit();
  102.     }
  103.     void FinalRelease()
  104.     {
  105.         IAccessorImpl<CMyProviderCommand>::FinalRelease();
  106.     }
  107.     HRESULT WINAPI Execute(IUnknown * pUnkOuter, REFIID riid, DBPARAMS * pParams,
  108.                           LONG * pcRowsAffected, IUnknown ** ppRowset);
  109.  
  110.     static ATLCOLUMNINFO* GetColumnInfo(CMyProviderCommand* pv, ULONG* pcInfo)
  111.     {
  112.         return CAgentMan::GetColumnInfo(pv, pcInfo);
  113.     }
  114.  
  115. BEGIN_PROPSET_MAP(CMyProviderCommand)
  116.     BEGIN_PROPERTY_SET(DBPROPSET_ROWSET)
  117.         PROPERTY_INFO_ENTRY_EX(IAccessor,VT_BOOL, DBPROPFLAGS_ROWSET | DBPROPFLAGS_READ, VARIANT_TRUE, 0)
  118.         PROPERTY_INFO_ENTRY_EX(IColumnsInfo, VT_BOOL,   DBPROPFLAGS_ROWSET | DBPROPFLAGS_READ, VARIANT_TRUE, 0)
  119.         PROPERTY_INFO_ENTRY_EX(IConvertType, VT_BOOL,   DBPROPFLAGS_ROWSET | DBPROPFLAGS_READ, VARIANT_TRUE, 0)
  120.         PROPERTY_INFO_ENTRY_EX(IRowset, VT_BOOL,    DBPROPFLAGS_ROWSET | DBPROPFLAGS_READ, VARIANT_TRUE, 0)
  121.         PROPERTY_INFO_ENTRY_EX(IRowsetIdentity,VT_BOOL, DBPROPFLAGS_ROWSET | DBPROPFLAGS_READ, VARIANT_TRUE, 0)
  122.         PROPERTY_INFO_ENTRY_EX(IRowsetInfo, VT_BOOL, DBPROPFLAGS_ROWSET | DBPROPFLAGS_READ, VARIANT_TRUE, 0)
  123.         PROPERTY_INFO_ENTRY_EX(IRowsetLocate,   VT_BOOL, DBPROPFLAGS_ROWSET | DBPROPFLAGS_READ | DBPROPFLAGS_WRITE, VARIANT_TRUE, 0)
  124.         PROPERTY_INFO_ENTRY_EX(BOOKMARKS,       VT_BOOL, DBPROPFLAGS_DATASOURCEINFO | DBPROPFLAGS_READ | DBPROPFLAGS_WRITE , VARIANT_TRUE, 0)
  125.         PROPERTY_INFO_ENTRY_EX(BOOKMARKSKIPPED,VT_BOOL, DBPROPFLAGS_DATASOURCEINFO | DBPROPFLAGS_READ, 0, 0)
  126.         PROPERTY_INFO_ENTRY_EX(BOOKMARKTYPE,    VT_I4, DBPROPFLAGS_DATASOURCEINFO | DBPROPFLAGS_READ, 0, 0)
  127.         PROPERTY_INFO_ENTRY_EX(CANFETCHBACKWARDS,VT_BOOL, DBPROPFLAGS_DATASOURCEINFO | DBPROPFLAGS_READ | DBPROPFLAGS_WRITE, 0, 0)
  128.         PROPERTY_INFO_ENTRY_EX(CANHOLDROWS, VT_BOOL, DBPROPFLAGS_DATASOURCEINFO | DBPROPFLAGS_READ | DBPROPFLAGS_WRITE, VARIANT_TRUE, 0)
  129.         PROPERTY_INFO_ENTRY_EX(CANSCROLLBACKWARDS, VT_BOOL, DBPROPFLAGS_DATASOURCEINFO | DBPROPFLAGS_READ, VARIANT_TRUE, 0)
  130.         PROPERTY_INFO_ENTRY_EX(LITERALBOOKMARKS, VT_BOOL, DBPROPFLAGS_ROWSET | DBPROPFLAGS_READ | DBPROPFLAGS_WRITE, VARIANT_TRUE, 0)
  131.         PROPERTY_INFO_ENTRY_EX(ORDEREDBOOKMARKS, VT_BOOL, DBPROPFLAGS_ROWSET | DBPROPFLAGS_READ | DBPROPFLAGS_WRITE, VARIANT_TRUE, 0)
  132.     END_PROPERTY_SET(DBPROPSET_ROWSET)
  133. END_PROPSET_MAP()
  134. };
  135.  
  136. template <class T, class Storage, class CreatorClass, class ArrayType = CSimpleArray<Storage> >
  137. class CMyRowsetImpl:
  138.     public CRowsetImpl<T, Storage, CreatorClass, ArrayType, CSimpleRow, IRowsetLocateImpl< T > >
  139. {
  140.  
  141. BEGIN_COM_MAP(CMyRowsetImpl)
  142.     COM_INTERFACE_ENTRY(IRowsetLocate)
  143.     COM_INTERFACE_ENTRY_CHAIN(_RowsetBaseClass)
  144. END_COM_MAP()
  145.  
  146.     // Implementation
  147.     HRESULT ValidateCommandID(DBID* pTableID, DBID* pIndexID)
  148.     {
  149.         HRESULT hr = _RowsetBaseClass::ValidateCommandID(pTableID, pIndexID);
  150.         if (hr != S_OK)
  151.             return hr;
  152.  
  153.         if (pIndexID != NULL)
  154.             return DB_E_NOINDEX;    // We don't support indexes
  155.  
  156.         return S_OK;
  157.     }
  158.  
  159. };
  160.  
  161.  
  162. class CAgentRowset : public CMyRowsetImpl<CAgentRowset, CAgentMan, CMyProviderCommand>
  163. {
  164. public:
  165.  
  166.     virtual DBSTATUS GetDBStatus(CSimpleRow* , HACCESSOR)
  167.     {
  168.         return DBSTATUS_S_ISNULL;
  169.     }
  170.  
  171.     HRESULT Execute(DBPARAMS * pParams, LONG* pcRowsAffected)
  172.     {
  173.         USES_CONVERSION;
  174.         FILE* pFile;
  175.         TCHAR szString[256];
  176.         TCHAR szFile[MAX_PATH];
  177.         size_t nLength;
  178.         ObjectLock lock(this);
  179.  
  180.  
  181.         // From a filename, passed in as a command text, scan the file placing data
  182.         // in the data array.
  183.         if (m_strCommandText == (BSTR)NULL)
  184.         {
  185.             ATLTRACE("No filename specified");
  186.             return E_FAIL;
  187.         }
  188.  
  189.         // Open the file
  190.         _tcscpy(szFile, OLE2T(m_strCommandText));
  191.         if (szFile[0] == _T('\0') || ((pFile = fopen(&szFile[0], "r")) == NULL))
  192.         {
  193.             ATLTRACE("Could not open file");
  194.             return DB_E_NOTABLE;
  195.         }
  196.  
  197.         // scan and parse the file.  The file should contain two strings per record
  198.         LONG cFiles = 0;
  199.         while (fgets(szString, 256, pFile) != NULL)
  200.         {
  201.             nLength = strlen(szString);
  202.             szString[nLength-1] = '\0'; // Strip off trailing CR/LF
  203.             CAgentMan am;
  204.             _tcscpy(am.szCommand, szString);
  205.             _tcscpy(am.szCommand2, szString);
  206.  
  207.             if (fgets(szString, 256, pFile) != NULL)
  208.             {
  209.                 nLength = strlen(szString);
  210.                 szString[nLength-1] = '\0'; // Strip off trailing CR/LF
  211.                 _tcscpy(am.szText, szString);
  212.                 _tcscpy(am.szText2, szString);
  213.             }
  214.  
  215.             am.dwBookmark = ++cFiles;
  216.             if (!m_rgRowData.Add(am))
  217.             {
  218.                 ATLTRACE("Couldn't add data to array");
  219.                 fclose(pFile);
  220.                 return E_FAIL;
  221.             }
  222.         }
  223.  
  224.         if (pcRowsAffected != NULL)
  225.             *pcRowsAffected = cFiles;
  226.         return S_OK;
  227.     }
  228.  
  229.     HRESULT OnPropertyChanged(ULONG iCurSet, DBPROP* pDBProp)
  230.     {
  231.         ATLASSERT(pDBProp != NULL);
  232.  
  233.         DWORD dwPropertyID = pDBProp->dwPropertyID;
  234.  
  235.         if (dwPropertyID == DBPROP_IRowsetLocate ||
  236.             dwPropertyID == DBPROP_LITERALBOOKMARKS ||
  237.             dwPropertyID == DBPROP_ORDEREDBOOKMARKS)
  238.         {
  239.             CComVariant var = pDBProp->vValue;
  240.  
  241.             if (var.boolVal == VARIANT_TRUE)
  242.             {
  243.                 // Set the bookmarks property as these are chained
  244.                 CComVariant bookVar(true);
  245.                 CDBPropSet set(DBPROPSET_ROWSET);
  246.                 set.AddProperty(DBPROP_BOOKMARKS, bookVar);
  247.  
  248.                 // If you set IRowsetLocate to true, then the rowset can
  249.                 // handle backward scrolling
  250.                 if (dwPropertyID == DBPROP_IRowsetLocate)
  251.                     set.AddProperty(DBPROP_CANSCROLLBACKWARDS, bookVar);
  252.  
  253.                 const GUID* ppGuid[1];
  254.                 ppGuid[0] = &DBPROPSET_ROWSET;
  255.  
  256.                 return SetProperties(0, 1, &set, 1, ppGuid);
  257.             }
  258.         }
  259.  
  260.         return S_OK;
  261.     }
  262. };
  263.  
  264.  
  265.  
  266. #endif //__CMyProviderRowset_H_
  267.